[Create a Namespace]
$ kubectl create namespace constraints-mem-example

[Create the LimitRange]
$ kubectl apply -f LimitRange_1.yaml --namespace=constraints-mem-example
$ kubectl apply -f https://k8s.io/examples/admin/resource/memory-constraints.yaml --namespace=constraints-mem-example

[View detailed information about the LimitRange]
$ kubectl get limitrange mem-min-max-demo-lr --namespace=constraints-mem-example --output=yaml
...
  limits:
  - default:
      memory: 1Gi
    defaultRequest:
      memory: 1Gi
    max:
      memory: 1Gi
    min:
      memory: 500Mi
    type: Container

Now whenever define a Pod within the constraints-mem-example namespace, Kubernetes performs these steps:-
If any container in that Pod does not specify its own memory request and limit, the control plane assigns the default memory request and limit to that container.
Verify that every container in that Pod requests at least 500 MiB of memory.
Verify that every container in that Pod requests no more than 1024 MiB (1 GiB) of memory.

[Create the Pod]
$ kubectl apply -f Pod_1.yaml --namespace=constraints-mem-example
$ kubectl apply -f https://k8s.io/examples/admin/resource/memory-constraints-pod.yaml --namespace=constraints-mem-example

[Verify that the Pod is running and that its container is healthy]
$ kubectl get pod constraints-mem-demo --namespace=constraints-mem-example

[View detailed information about the Pod]
$ kubectl get pod constraints-mem-demo --output=yaml --namespace=constraints-mem-example
...
resources:
  limits:
     memory: 800Mi
  requests:
    memory: 600Mi

[Delete the Pod]
$ kubectl delete pod constraints-mem-demo --namespace=constraints-mem-example


⦿ Attempt to create a Pod that exceeds the maximum memory constraint

[Create the Pod]
$ kubectl apply -f Pod_2.yaml --namespace=constraints-mem-example
$ kubectl apply -f https://k8s.io/examples/admin/resource/memory-constraints-pod-2.yaml --namespace=constraints-mem-example
Error from server (Forbidden): error when creating "examples/admin/resource/memory-constraints-pod-2.yaml": pods "constraints-mem-demo-2" is forbidden: maximum memory usage per Container is 1Gi, but limit is 1536Mi.

The output shows that the Pod does not get created, because it defines a container that requests more memory than is allowed.


⦿ Attempt to create a Pod that does not meet the minimum memory request

[Create the Pod]
$ kubectl apply -f Pod_3.yaml --namespace=constraints-mem-example
$ kubectl apply -f https://k8s.io/examples/admin/resource/memory-constraints-pod-3.yaml --namespace=constraints-mem-example
Error from server (Forbidden): error when creating "examples/admin/resource/memory-constraints-pod-3.yaml": pods "constraints-mem-demo-3" is forbidden: minimum memory usage per Container is 500Mi, but request is 100Mi.

The output shows that the Pod does not get created, because it defines a container that requests less memory than the enforced minimum.


⦿ Create a Pod that does not specify any memory request or limit 

[Create the Pod]
$ kubectl apply -f Pod_4.yaml --namespace=constraints-mem-example
$ kubectl apply -f https://k8s.io/examples/admin/resource/memory-constraints-pod-4.yaml --namespace=constraints-mem-example

[View detailed information about the Pod]
$ kubectl get pod constraints-mem-demo-4 --namespace=constraints-mem-example --output=yaml
...
resources:
  limits:
    memory: 1Gi
  requests:
    memory: 1Gi

Because the Pod did not define any memory request and limit for that container, the cluster applied a default memory request and limit from the LimitRange.

$ kubectl describe pod constraints-mem-demo-4 --namespace=constraints-mem-example

At this point, your Pod might be running or it might not be running. 
Recall that a prerequisite for this task is that your Nodes have at least 1 GiB of memory. 
If each of your Nodes has only 1 GiB of memory, then there is not enough allocatable memory on any Node to accommodate a memory request of 1 GiB. 
If you happen to be using Nodes with 2 GiB of memory, then you probably have enough space to accommodate the 1 GiB request.

[Delete the Pod]
$ kubectl delete pod constraints-mem-demo-4 --namespace=constraints-mem-example

[Delete the Namespace]
$ kubectl delete namespace constraints-mem-example
